<template>
	<view class="content">
		<post-list-cell class="forum_post_cell_content" v-for="(item,index) in items" :key="index" :item="item">
		</post-list-cell>
	</view>
</template>

<script>
	import http from "../../utils/http.js"
	import util from "../../utils/util.js"
	export default {
		data() {
			return {
				items: [],
				type: "",
				uid: "",
				page: 1
			}
		},
		onLoad(option) {
			this.type = option.type ? option.type : "";
			this.uid = option.uid ? option.uid : "";

			if (this.type == "topic") {
				uni.setNavigationBarTitle({
					title: "发表的"
				})
			} else if (this.type == "favorite") {
				uni.setNavigationBarTitle({
					title: "收藏的"
				})
			} else if (this.type == "reply") {
				uni.setNavigationBarTitle({
					title: "回复的"
				})
			}


			this.loadData();
		},
		onPullDownRefresh() {
			this.loadData();
		},
		onReachBottom() {
			this.loadMoreData();
		},
		methods: {
			loadData() {
				var that = this;
				http.request("user/topiclist", {
					pageSize: 20,
					page: 1,
					uid: this.uid,
					type: this.type
				}, {
					success: function(data) {
						uni.stopPullDownRefresh();
						that.items = data.list.map(function(item) {
							item.showTime = util.dateStringFromTimestamp(item.last_reply_date);
							return item;
						})
						that.page = 2;
					},
					fail: function(msg) {
						uni.stopPullDownRefresh();
						uni.showToast({
							title: msg,
							icon: "none"
						})
					}
				})
			},
			loadMoreData() {
				var that = this;
				http.request("user/topiclist", {
					pageSize: 20,
					page: this.page,
					uid: this.uid,
					type: this.type
				}, {
					success: function(data) {
						var items = that.items.concat(data.list.map(function(item) {
							item.showTime = util.dateStringFromTimestamp(item.last_reply_date);
							return item;
						}))
						that.items = items;
						that.page++;
					},
					fail: function(msg) {
						uni.showToast({
							title: msg,
							icon: "none"
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.content {
		display: flex;
		width: 100%;
		flex-direction: column;
	}

</style>
